<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>_${customer.customerName}_上架</title>
<style type="text/css">
	.chosen-container {
		width: 100%!important;
	}
</style>
<script type="text/javascript" src="${ctx}/static/v2/js/modal.js"></script>
<script type="text/javascript">

  $(function() {
       //如果上架已经完成，隐藏操作按钮
	   var status='${task.status}';
	   if(status!='token' && status!='doing'){
	      $("button").hide();   
	   }
	   $("#upForm").validate();
  });
  function clearBatch() {
  	$('#txt_batchCode').val('');
	$('#txt_productDate').val('');
	$('#txt_expireDate').val('');
  	$('.txt_batchCode').css('display','none');
  	$('.txt_expireDate').css('display','none');
  }
  /*function keydown_key() {
  	$('#txt_skuCode').blur();
  }*/
  function keydown() {
  	$('#txt_skuCode').val('');
  	$('#txt_skuNum').val('');
  	$('#txt_boxCode').val('');
  	$('#txt_stockCode').val('');
  	$('#txt_skuCode').focus();
  }
  var batchTime = 0;
	function keydown_key() {
		keydown_1();
	}
	function checkBatch() {
		if (batchTime == 0) {
			keydown_1();
		}
	}
  function keydown_1(){
	  	var customerCode='${task.customerCode}';
	  	var subTaskCode='${task.subTaskCode}';
	  	var billCode='${task.billCode}';
	  	var sku = $("#txt_skuCode").val().trim();
	  	$.ajax({
			type:'post',
			url:'${ctx}/v2_instorageUp/findSku',
			data:{
				subTaskCode:subTaskCode,
				billCode:billCode,
				customerCode:customerCode,
				sku:sku
			},
			dataType:'json',
			success:function(data) {
				batchTime = 1;
				if(data.status == "success"){
					if(data.isShelfLifeMgmt != 0 || data.isBatchMgmt != 0){
						var batchCode_modal, productDate_modal, expireDate_modal;
						batchCode_modal = data.batchList[0].batch_code == undefined ? '' : data.batchList[0].batch_code;
						productDate_modal = data.batchList[0].product_date == undefined ? '' : data.batchList[0].product_date;
						expireDate_modal = data.batchList[0].expire_date == undefined ? '' : data.batchList[0].expire_date;
						if (data.isBatchMgmt != 0) {
							$("#txt_batchCode").val(batchCode_modal);
							$('.txt_batchCode').css('display','inline-block');
						}
						if (data.isShelfLifeMgmt != 0) {
							$("#txt_productDate").val(productDate_modal);
							$("#txt_expireDate").val(expireDate_modal);
							$('.txt_expireDate').css('display','inline-block');
						}
						$("#txt_skuNum").focus();
					}else{
						batchCode = '';
						productDate = '';
						expireDate = '';
						$("#txt_skuNum").focus();
					}
			    	
			    	
			    }else if(data.status == "skuNotExist"){
			       alert(data.content);
			       $("#txt_skuCode").val('');
			       $("#txt_skuCode").focus();
			    }else {
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   	});
  }
  
  function stockCodeChange(obj){
  	if(!$("#upForm").valid()){
  		return;
  	}
	  var boxCode=$("#txt_boxCode").val().trim();
	  var stockCode=$("#txt_stockCode").val().trim();
	  var t = /^[A-Z0-9\.-]*$/g;
	  var t1 = /^[A-Z0-9\.-]*$/g;
	  
	  if(!t.test(boxCode)){
		jAlert("箱编码只能输入大写字母,数字,中划线.");
		return;
	  }
		  
	  if(!t1.test(stockCode)){
		jAlert("位编码只能输入大写字母,数字,中划线.");
		return;
	  }
	  var skuCode=$("#txt_skuCode").val().trim();
	  var tmpskuCode=skuCode;
	  skuCode = skuCode.replace('/', '\\/');
	  var batchCode = $("#txt_batchCode").val().trim();
	  var productDate = $("#txt_productDate").val().trim();
	  var expireDate = $("#txt_expireDate").val().trim();
	  var skuNum=$("#txt_skuNum").val().trim();
	  var main_num_good = $("#main_num_good_"+skuCode).val() ;
	  var main_num_bad = $("#main_num_bad_"+skuCode).val() ;
	  
	  if(parseInt(main_num_good)<parseInt(skuNum)||parseInt(main_num_bad)<parseInt(skuNum)){
		  jAlert("本次上架数量超过可上架数量.");
	  	  return;
	  }
	  var errorFlag=$("#error_flag").val();
	  /* if(!($("#sku_"+skuCode).html() && $("#batch_"+batchCode).html() 
			  && $("#product_"+productDate).html() && $("#expire_"+expireDate).html())){
	  	jAlert("SKU不在上架列表中.");
  		return;
  	  } */
	  if(!($("#sku_"+skuCode).html())){
	  	jAlert("SKU不在上架列表中.");
	  	$('#txt_skuNum').val('');
	  	$('#txt_boxCode').val('');
	  	$('#txt_stockCode').val('');
	  	$('#txt_skuCode').val('');
	  	$('#txt_skuCode').focus();
  		return;
  	  }
  	  
  	  if(errorFlag==0){
  	  	  $("#msg").css("color","red");
		  $("#msg").html("请选择报损状态!");
		  $("#error_flag").focus();
		  return;
  	  }
   	  if(main_num_good == undefined&&errorFlag == "noterror"){
		  jAlert("无此完好件.");
	  	  return;
	  }
	  if(main_num_bad == undefined&&errorFlag == "error"){
		  jAlert("无此破损件");
		  return;
	  }
	  if(boxCode.length==0){
		  $("#msg").css("color","red");
		  $("#msg").html("请录入/扫描箱编码!");
		  $("#txt_boxCode").focus();
	  }
	  
	  if(stockCode.length==0){
		  $("#msg").css("color","red");
		  $("#msg").html("请录入/扫描库位编码!");
		  $("#txt_stockCode").select();
		  $("#txt_stockCode").focus();
	  }
	  
	  var uptaskCode='${task.taskCode}';
	  $(obj).attr("disabled",true);
      $.ajax({
			type:'post',
			url:'${ctx}/v2_instorageUp/ajaxUpNew/${task.id}',
			data:{
				upTaskCode:uptaskCode,
				boxCode:boxCode,
				stockCode:stockCode,
				skuCode:tmpskuCode,
				batchCode:batchCode,
				productDate:productDate,
				expireDate:expireDate,
				num:skuNum,
				errorFlag:errorFlag
			},
			dataType:'json',
			success:function(data) {
				/**add 上架过程中将不在收货任务中的sku识别出来，增加提示   bug404 by zhangweize   L318-329（除了323)\L335-337*/
				if ( data.status == "error") {
					  jAlert(data.msg);
				}
				batchTime = 0;
				jAlert("上架成功!"); 
				window.location.href = "${ctx}/v2_instorageUp/toInstorageUpPageNew/${task.id}";
			},
			error:function() {
				$(obj).attr("disabled",false);
				alert("网络繁忙，请稍后重试...");
			}
	   });
  }
  function finishUpTask(item){
  		$(item).attr("disabled",true);
		if($("#main_1").html() == '0'){
			 $.ajax({
				type:'post',
				url:'${ctx}/v2_instorageUp/finishUpTask/${task.taskCode}',
				dataType:'json',
				success:function(data) {
					var msg=data.msg;
					$(item).attr("disabled",false);
				    if( msg=='continue'){
					    $("#msg").css("color","green");
			            $("#msg").html("上架成功.");
			            
			            $("#upStatus_"+boxCode).html("<font color=green>已上架</font>");
			            
				    }
				    else if(msg=='finishing'){
				        $("#msg").css("color","green");
			            $("#msg").html("上架成功.");
			            
			            jAlert("完成上架!"); 
					    location.href='${ctx}/v2_instorageUp/toUpTaskListPage';
			            
				    }
				    else if(msg=='uped'){
				        $("#msg").css("color","red");
			            $("#msg").html("已上架！");
			            
			            $("#txt_boxCode").val('');
			            $("#txt_stockCode").val('');
			            $("#txt_boxCode").focus();
				    }
				    else if(msg=='finish'){
				        $("#msg").css("color","red");
			            $("#msg").html("已上架！");
			            
			            $("#txt_boxCode").val('');
			            $("#txt_stockCode").val('');
			            $("#txt_boxCode").focus();
				    }
				    else if(msg=='S009'){
				        $("#msg").css("color","red");
			            $("#msg").html("入库单已取消");
			            
			            $("#txt_boxCode").val('');
			            $("#txt_stockCode").val('');
			            $("#txt_boxCode").focus();
			       }else if(msg=='S1002'){
				        $("#msg").css("color","red");
			            $("#msg").html("上架数量和分配数量不一致.请核对");
			            
			            $("#txt_boxCode").val('');
			            $("#txt_stockCode").val('');
			            $("#txt_boxCode").focus();
				    }else if(msg=='isEmpty'){
				    	$("#msg").css("color","red");
			            $("#msg").html("入库单已上架");
			            
			            $("#txt_boxCode").val('');
			            $("#txt_stockCode").val('');
			            $("#txt_boxCode").focus();
				    }else if(msg=='notMatch'){
				    	$("#msg").css("color","red");
			            $("#msg").html("SKU、批次号、生产日期、过期日期不匹配！");
			            
			            $("#txt_boxCode").val('');
			            $("#txt_stockCode").val('');
			            $("#txt_boxCode").focus();
				    }
				    else{
				        $("#msg").css("color","red");
			            $("#msg").html("验证不成功！");
			            
			            $("#txt_boxCode").val('');
			            $("#txt_stockCode").val('');
			            $("#txt_boxCode").focus();
					}
				},
				error:function() {
					alert("网络繁忙，请稍后重试...");
				}
		   });
		}else{
			jAlert("还有未上架的SKU.");
		}
	}
</script>
</head>

<body>
<section class="content">
	<form id="upForm">
	<table id="contentTable" class="table  table-bordered table-condensed">
	    <thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#3c8dbc"><i class="fa fa-barcode"/>  扫描上架</th>
		</thead>
		
		<tr>
		    <td > 
		    	<span style="margin-left: 10px;">是否报损件:</span>
		       	<select id="error_flag" class="oldSelectStyle" onchange="keydown();">
				      <option value="noterror">完好件</option>   
				     <option value="error">破损件</option>  
				    
				</select>
		    
		       <span style="margin-left: 10px;">SKU:</span>
		       <input id="txt_skuCode" class="required" name="txt_skuCode" type="text"  onkeydown="if(event.keyCode == '13'){keydown_key();}" onchange="clearBatch();"/>
		       <div style="display:none;" class="txt_batchCode">
			       <span style="margin-left: 10px;">批次号:</span>
			       <input id="txt_batchCode" class="required" name="txt_batchCode" type="text" disabled />
			    </div>
		       <div style="display:none;" class="txt_expireDate">
			       <span style="margin-left: 10px;">生产日期:</span>
			       <input id="txt_productDate" class="required" name="txt_productDate" type="text" disabled/>
			    </div>
		       <div style="display:none;" class="txt_expireDate">
			       <span style="margin-left: 10px;">过期日期:</span>
			       <input id="txt_expireDate" class="required" name="txt_expireDate" type="text" disabled/>
			    </div>
		       
		       <span style="margin-left: 10px;">数量:</span>
		       <input id="txt_skuNum" class="required digits" name="txt_skuNum" type="text" oninput="checkBatch()" />
		       
		       <span style="margin-left: 10px;">箱编码:</span>
		       <input id="txt_boxCode" class="required" name="txt_boxCode" type="text"  oninput="checkBatch()" />
		       
		       <span style="margin-left: 10px;">位编码:</span>
		       <input id="txt_stockCode" class="required" name="txt_stockCode" type="text" oninput="checkBatch()"/>
		       
		       <span style="margin-left:12px; margin-top: 10px; color: red;font-weight: bold;" id="msg"></span>
		       <span style="margin-left:12px;"><a class="btn btn-sm btn-primary" onclick="stockCodeChange(this);">上架</a></span>
		    </td>
		</tr>
		
	</table>
	</form>
	<!-- 已上架数据 -->
	<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		<thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#3c8dbc"><i class="fa fa-th-list"/>  已上架商品数据</th>
		</thead>
		<thead><tr>
		        <th style="width: 130px;">序号</th>
		        <th style="width: 130px;">报损状态</th>
			    <th style="width: 130px;">SKU</th>
			    <th style="width: 130px;">批次号</th>
			    <th style="width: 130px;">生产日期</th>
			    <th style="width: 130px;">过期日期</th>
			    <th style="width: 130px;">数量</th>
			    <th style="width: 130px;">箱编码</th>
			    <th style="width: 130px;">位编码</th>
			    <th style="width: 130px;">操作</th>
			</tr></thead>
		<tbody id="upBoxItem">
		<c:forEach items="${itemList}" var="t" varStatus="i">
			<tr>
			   <td >${i.count}</td>
			   <c:if test="${t.errorFlag eq 'noterror'}">
					<td>完好件</td>
				</c:if>
				<c:if test="${t.errorFlag eq 'error'}">
					<td>破损件</td>
				</c:if>
			   <td>${t.skuCode}</td>
			   <td>${t.batchCode}</td>
			   <td>${t.productDate}</td>
			   <td>${t.expireDate}</td>
			   <td>${t.num}</td>
			   <td>${t.boxCode}</td>
			   <td>${t.stockCode}</td>
			   <td><a class="btn btn-sm btn-danger" href="${ctx}/v2_instorageUp/deleteItem/${task.id}/${t.id}">删除</a></td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<!-- 待上架数据 -->
	<table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		<thead>
			<th style="vertical-align: middle; color: white; " colspan="13" bgcolor="#3c8dbc"><i class="fa fa-th-list"/>  商品数据</th>
		</thead>
		<thead><tr>
		        <th style="width: 120px;">序号</th>
		        <th  style="width: 120px;">报损状态</th>
			    <th style="width: 120px;">SKU</th>
			    <th style="width: 120px;">批次号</th>
			    <th style="width: 120px;">生产日期</th>
			    <th style="width: 120px;">过期日期</th>
			    <th style="width: 120px;">应上架数量</th>
			    <th style="width: 120px;">未上架数量</th>
			</tr></thead>
		<tbody id="boxItem">
		<c:forEach items="${unUpItem}" var="t" varStatus="i">
			<tr>
			   <td >${i.count}</td>
			   	<c:if test="${t.errorFlag eq 'noterror'}">
					<td>完好件<input type="hidden" id="main_num_good_${t.skuCode.split("_")[0]}" value="${t.num-t.upNum}"></td>
				</c:if>
				<c:if test="${t.errorFlag eq 'error'}">
					<td>破损件<input type="hidden" id="main_num_bad_${t.skuCode.split("_")[0]}" value="${t.num-t.upNum}"></td>
				</c:if>
			   <td id="sku_${t.skuCode.split("_")[0]}">${t.skuCode.split("_")[0]}</td>
			   <td id="batch_${t.batchCode}">${t.batchCode}</td>
			   <td id="product_${t.productDate}">${t.productDate}</td>
			   <td id="expire_${t.expireDate}">${t.expireDate}</td>
			   <td>${t.num}</td>
			   <td id="main_${i.count }">${t.num-t.upNum}</td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div class="modal-footer bg-gray">
			<a class="btn btn-primary" onclick="finishUpTask(this)" >完成上架</a>
		</div>
</section>	
</body>
</html>
